//
//  NSString+Util.m
//  SmallSecretary1.5
//
//  Created by wangzhi on 14-8-20.
//  Copyright (c) 2014年 pretang. All rights reserved.
//

#import "NSString+Util.h"
#import "RegexKitLite.h"

@implementation NSString (Util)

+ (NSString *)localStringForKey:(NSString *)key
{
    return NSLocalizedString(key, nil);
}

+ (NSString *)localStringForKey:(NSString *)key comment:(NSString *)comment
{
    return NSLocalizedString(key, comment);
}

+ (NSString *)localStringForKey:(NSString *)key comment:(NSString *)comment table:(NSString *)table
{
    return NSLocalizedStringFromTable(key, table, comment);
}

- (BOOL)isPhoneNumber
{
    
//    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSString *phoneRegex = @"^1([0-9]{10})";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    BOOL test = [phoneTest evaluateWithObject:self];
    return test;
//    if (test == YES) {
//        return YES;
//    }else
//    {
//        if (self.length == 14) {
//            if ([[self substringToIndex:3] isEqualToString:@"＋86"]||[[self substringToIndex:3] isEqualToString:@"+86"]) {
//                NSString *str = [self substringWithRange:NSMakeRange(3, 11)];
//                return  [phoneTest evaluateWithObject:str];
//            }else
//            {
//                return NO;
//            }
//        }else
//        {
//            return NO;
//        }
//     
//    }
//    return [phoneTest evaluateWithObject:self];
}

- (NSDate *)dateFromeSystemDateStringWithFormat:(NSString *)format
{
    return [NSDate dateWithTimeIntervalSince1970:self.intValue];
}

//判断是否为整形
- (BOOL)isPureInt{
    NSScanner* scan = [NSScanner scannerWithString:self];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

//判断是否为浮点形
- (BOOL)isPureFloat{
    NSScanner* scan = [NSScanner scannerWithString:self];
    float val;
    return[scan scanFloat:&val] && [scan isAtEnd];
}

- (BOOL)isValidNumber
{
    if ([self isMatchedByRegex:@"^((0)|(0[.][0-9]*)|([1-9]{1}[0-9]*[.]?[0-9]*))$"])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (BOOL)isValidPersonId
{
    return [self isMatchedByRegex:@"^([0-9]{18}|[0-9]{15}|[0-9]{17}[x|X])$"];
}

+ (NSString *)dateStringWithInterval:(NSTimeInterval)timeVal formatStr:(NSString *)formatStr
{
    if (timeVal == 0)
    {
        return nil;
    }
    NSDateFormatter *formater = [[NSDateFormatter alloc]init];
    [formater setDateFormat:formatStr];

    return [[self class]dateStringWithInterval:timeVal format:formater];
}

+ (NSString *)dateStringWithInterval:(NSTimeInterval)timeVal format:(NSDateFormatter *)formater
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeVal/1000];
    NSMutableString *dateStr = [[NSMutableString alloc]init];
    [dateStr setString:[formater stringFromDate:date]];

    [dateStr replaceOccurrencesOfRegex:@"PM" withString:@"下午"];
    [dateStr replaceOccurrencesOfRegex:@"AM" withString:@"上午"];

    return dateStr;
}

+ (NSString*)timeInterval:(NSDate*)date
{
    NSTimeInterval timeInterVal = [date timeIntervalSince1970];
    timeInterVal = timeInterVal * 1000;
    return [NSString stringWithFormat:@"%f", timeInterVal];
}

-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    NSDictionary *attribute = @{NSFontAttributeName:font};
    CGSize fitSize = [self boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return fitSize;
}

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
{
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.lineBreakMode = lineBreakMode;
    NSDictionary *attribute = @{NSFontAttributeName:font, NSParagraphStyleAttributeName: paragraph};

    CGSize fitSize = [self boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return fitSize;
}

-(CGSize)sizeWithFont:(UIFont *)font
{
    NSDictionary *attribute = @{NSFontAttributeName:font};
    return [self sizeWithAttributes:attribute];
}

- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment
{
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.lineBreakMode = lineBreakMode;
    paragraph.alignment = alignment;
    NSDictionary *attribute = @{NSFontAttributeName:font, NSParagraphStyleAttributeName: paragraph};

    [self drawInRect:rect withAttributes:attribute];

    return rect.size;
}

- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode
{
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.lineBreakMode = lineBreakMode;
    NSDictionary *attribute = @{NSFontAttributeName:font, NSParagraphStyleAttributeName: paragraph};

    [self drawInRect:rect withAttributes:attribute];

    return rect.size;
}

- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font
{
    NSDictionary *attribute = @{NSFontAttributeName:font};

    [self drawInRect:rect withAttributes:attribute];

    return rect.size;
}

- (BOOL)containsSubString:(NSString*)string
{
    return ([self rangeOfString:string].length > 0);
}

-(NSString *)cutStringAtIndex:(int)index{
    NSString *temp = self;
    if (self.length > index) {
        temp = [temp substringToIndex:index];
        temp = [temp stringByAppendingString:@"..."];
    }
    return temp;
}

- (NSString *)replaceBlankValueWithString:(NSString *)string {
    if (!self || [self isEqualToString:@""]) {
        return string;
    }
    return self;
}
@end
